<?php

/**
 * games actions.
 *
 * @package    ghostly
 * @subpackage games
 * @author     Stefan Moonen <stefan@qonstrukt.nl>
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class gamesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $display_length = $request->getParameter('iDisplayLength', sfConfig::get('app_games_rows_per_page'));
    $display_start = $request->getParameter('iDisplayStart', 0) / $display_length + 1;

    $this->pager = new sfDoctrinePager(
      'Game',
      $display_length
    );
    $this->pager->setQuery(GameTable::getInstance()->getOverviewQuery());
    $this->pager->setPage($display_start);
    $this->pager->init();

    if ($request->isXmlHttpRequest())
    {
      $data = array();
      foreach ($this->pager->getResults() as $row)
      {
        $data[] = array(
          $row->datetime,
          $row->gamename,
          $row->niceMap,
          $row->shortDuration
        );
      }

      $output = array(
        'echo' => $request->getParameter('echo'),
        'iTotalRecords' => $this->pager->getNbResults(),
        'iTotalDisplayRecords' => $this->pager->getNbResults(),
        'aaData' => $data
      );

      exit(json_encode($output));
    }
  }

 /**
  * Executes getData action
  *
  * @param sfRequest $request A request object
  */
  public function executeGetData(sfWebRequest $request)
  {
    $this->games = GameTable::getInstance();

    $this->pager = new sfDoctrinePager(
      'Game',
      sfConfig::get('app_games_rows_per_page')
    );
    $this->pager->setQuery($this->games->getOverviewQuery());
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }
}
